package com.hfjava.practice06;

import java.io.File;
import java.util.Date;

/**
 * @author Administrator
 * @version V1.0
 * @ProjectName:IntelliJ IDEA
 * @PackageName: com.hfjava.practice06
 * @ClassName: FileTest
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2019/12/26 9:28
 * @Copyright 大牛版板所有
 */
public class FileTest {
    public static void main(String[] args)throws Exception {
        String filePath = "G:/MyFolder";
        String fileName = "test.txt";
        File myFolder = new File(filePath);
        if (!myFolder.exists()){
            myFolder.mkdirs();
        }

        File myFile = new File(filePath+"/"+fileName);
        if (!myFile.exists()){
            myFile.createNewFile();
        }
        System.out.println("===============================================");
        System.out.println();
        System.out.println("文件绝对路径:"+myFile.getAbsolutePath());
        System.out.println("文件名称:"+myFile.getName());
        System.out.println("文件父节点:"+myFile.getParent());
        System.out.println("是文件夹吗？:"+myFile.isDirectory());
        System.out.println("是文件吗？:"+myFile.isFile());
        System.out.println("是隐藏的吗？:"+myFile.isHidden());
        System.out.println("是可读的吗？:"+myFile.canRead());
        System.out.println("是可写的吗？:"+myFile.canWrite());
        System.out.println("是可执行的吗？:"+myFile.canExecute());
        System.out.println("最后一次修改时间？:"+new Date(myFile.lastModified()));
        System.out.println("文件的大小:"+myFile.length() +"字节");
        System.out.println("------------------------");
        
        File[] allFilePath = myFolder.listFiles();
        System.out.println("文件：");
        for (File file:allFilePath) {
            if (file.isFile()){
                System.out.println("\t"+file.getName());
            }
        }

        System.out.println("文件夹：");
        for (File file:allFilePath) {
            if (file.isDirectory()){
                System.out.println("\t"+file.getName());
            }
        }

        System.out.println("\n=========================================================");
        File[] roots = File.listRoots();
        for (int i = 0; i < roots.length; i++) {
            System.out.println(roots[i]);
            System.out.println("空闲空间 " + roots[i].getFreeSpace());
            System.out.println("可用空间 " + roots[i].getUsableSpace());
            System.out.println("总空间" + roots[i].getTotalSpace());
            System.out.println();
        }
        System.out.println("=========================================================");

        myFile.delete();
        if (!myFile.exists()){
            System.out.println("文件删除成功");
        }
        myFolder.delete();
        if (!myFolder.exists()){
            System.out.println("文件夹删除成功");
        }
        System.out.println("=========================================================");
    }
}
